package com.micangcaihang.micang.mvp.task.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.compress.CompressConfig;
import com.luck.picture.lib.compress.CompressImageOptions;
import com.luck.picture.lib.compress.CompressInterface;
import com.luck.picture.lib.compress.Luban;
import com.luck.picture.lib.compress.LubanOptions;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.micangcaihang.common.base.fragment.BaseFragment;
import com.micangcaihang.common.baserx.RxSchedulers;
import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.common.utils.LoadDialogUtil;
import com.micangcaihang.common.utils.StringUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.adapter.AccessoryInfoAdapter;
import com.micangcaihang.micang.databinding.FragmentTaskBorrowerAccessoryFBinding;
import com.micangcaihang.micang.entity.EventCenter;
import com.micangcaihang.micang.entity.TaskAccessoryInfoEntity;
import com.micangcaihang.micang.entity.UpLoadPicEntity;
import com.micangcaihang.micang.entity.UserInfoSp;
import com.micangcaihang.micang.http.ApiConstant;
import com.micangcaihang.micang.http.MiSubScriber;
import com.micangcaihang.micang.http.callback.JsonCallback;
import com.micangcaihang.micang.http.request.TaskAccessoryInfoRequest;
import com.micangcaihang.micang.mvp.task.contract.FTaskBorrowerAccessoryContract;
import com.micangcaihang.micang.mvp.task.presenter.FTaskBorrowerAccessoryPresenter;
import com.micangcaihang.micang.view.PickerViewHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/24
 *     update : 2017/07/24
 *     desc   : 风控 借款人附件
 *     version: 1.0
 * </pre>
 */

public class FTaskBorrowerAccessoryFragment extends BaseFragment<FTaskBorrowerAccessoryPresenter,
        FragmentTaskBorrowerAccessoryFBinding> implements FTaskBorrowerAccessoryContract.View,
        AccessoryInfoAdapter.onAddPicClickListener, AccessoryInfoAdapter.OnItemClickListener {

    private AccessoryInfoAdapter mAdapter;
    private String id;
    private TaskAccessoryInfoRequest mRequest;
    private int parentPos;
    private String marriage_status = "0";//婚姻状态
    private List<TaskAccessoryInfoEntity> mList;//原始数据
    private int position;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getString("id");
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public static FTaskBorrowerAccessoryFragment newInstance(String id) {
        Bundle args = new Bundle();
        FTaskBorrowerAccessoryFragment fragment = new FTaskBorrowerAccessoryFragment();
        args.putString("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * s收到婚姻状态更改的消息,改变适配器数据
     *
     * @param center
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventCenter center) {
        List<TaskAccessoryInfoEntity> newList = new ArrayList<>();
        newList.addAll(mList);
        updateAdapter(center, newList);
    }

    private void updateAdapter(EventCenter center, List<TaskAccessoryInfoEntity> newList) {
        switch (center.getEventCode()) {
            case EventCenter.TASK_MARRIAGE_STATUS1:
                marriage_status = "1";
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("50".equals(entity.getFile_type_id()) ||
                            "51".equals(entity.getFile_type_id()) ||
                            "52".equals(entity.getFile_type_id()) ||
                            "53".equals(entity.getFile_type_id()) ||
                            "54".equals(entity.getFile_type_id()) ||
                            "55".equals(entity.getFile_type_id()) ||
                            "56".equals(entity.getFile_type_id()) ||
                            "57".equals(entity.getFile_type_id()) ||
                            "58".equals(entity.getFile_type_id()) ||
                            "59".equals(entity.getFile_type_id()) ||
                            "60".equals(entity.getFile_type_id()) ||
                            "61".equals(entity.getFile_type_id()) ||
                            "62".equals(entity.getFile_type_id()) ||
                            "63".equals(entity.getFile_type_id()) ||
                            "64".equals(entity.getFile_type_id()) ||
                            "65".equals(entity.getFile_type_id()) ||
                            "66".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;
                    }
                }
                mAdapter.setNewData(newList);
                break;
            case EventCenter.TASK_MARRIAGE_STATUS2:
                marriage_status = "2";
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("56".equals(entity.getFile_type_id()) ||
                            "57".equals(entity.getFile_type_id()) ||
                            "58".equals(entity.getFile_type_id()) ||
                            "59".equals(entity.getFile_type_id()) ||
                            "60".equals(entity.getFile_type_id()) ||
                            "61".equals(entity.getFile_type_id()) ||
                            "62".equals(entity.getFile_type_id()) ||
                            "63".equals(entity.getFile_type_id()) ||
                            "64".equals(entity.getFile_type_id()) ||
                            "65".equals(entity.getFile_type_id()) ||
                            "66".equals(entity.getFile_type_id()) ||
                            "67".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;
                    }
                }
                mAdapter.setNewData(newList);
                break;
            case EventCenter.TASK_MARRIAGE_STATUS3://再婚
                marriage_status = "3";
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("50".equals(entity.getFile_type_id()) ||
                            "51".equals(entity.getFile_type_id()) ||
                            "52".equals(entity.getFile_type_id()) ||
                            "53".equals(entity.getFile_type_id()) ||
                            "54".equals(entity.getFile_type_id()) ||
                            "55".equals(entity.getFile_type_id()) ||
                            "56".equals(entity.getFile_type_id()) ||
                            "57".equals(entity.getFile_type_id()) ||
                            "58".equals(entity.getFile_type_id()) ||
                            "59".equals(entity.getFile_type_id()) ||
                            "67".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;
                    }
                }
                mAdapter.setNewData(newList);
                break;
            case EventCenter.TASK_MARRIAGE_STATUS4://离异
                marriage_status = "4";
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("50".equals(entity.getFile_type_id()) ||
                            "51".equals(entity.getFile_type_id()) ||
                            "52".equals(entity.getFile_type_id()) ||
                            "53".equals(entity.getFile_type_id()) ||
                            "54".equals(entity.getFile_type_id()) ||
                            "55".equals(entity.getFile_type_id()) ||
                            "58".equals(entity.getFile_type_id()) ||
                            "59".equals(entity.getFile_type_id()) ||
                            "60".equals(entity.getFile_type_id()) ||
                            "61".equals(entity.getFile_type_id()) ||
                            "62".equals(entity.getFile_type_id()) ||
                            "63".equals(entity.getFile_type_id()) ||
                            "64".equals(entity.getFile_type_id()) ||
                            "65".equals(entity.getFile_type_id()) ||
                            "66".equals(entity.getFile_type_id()) ||
                            "67".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;
                    }
                }
                mAdapter.setNewData(newList);
                break;
            case EventCenter.TASK_MARRIAGE_STATUS5:
                marriage_status = "5";
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("50".equals(entity.getFile_type_id()) ||
                            "51".equals(entity.getFile_type_id()) ||
                            "52".equals(entity.getFile_type_id()) ||
                            "53".equals(entity.getFile_type_id()) ||
                            "54".equals(entity.getFile_type_id()) ||
                            "55".equals(entity.getFile_type_id()) ||
                            "56".equals(entity.getFile_type_id()) ||
                            "57".equals(entity.getFile_type_id()) ||
                            "60".equals(entity.getFile_type_id()) ||
                            "61".equals(entity.getFile_type_id()) ||
                            "62".equals(entity.getFile_type_id()) ||
                            "63".equals(entity.getFile_type_id()) ||
                            "64".equals(entity.getFile_type_id()) ||
                            "65".equals(entity.getFile_type_id()) ||
                            "66".equals(entity.getFile_type_id()) ||
                            "67".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;
                    }
                }
                mAdapter.setNewData(newList);
                break;
            default:
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("50".equals(entity.getFile_type_id()) ||
                            "51".equals(entity.getFile_type_id()) ||
                            "52".equals(entity.getFile_type_id()) ||
                            "53".equals(entity.getFile_type_id()) ||
                            "54".equals(entity.getFile_type_id()) ||
                            "55".equals(entity.getFile_type_id()) ||
                            "56".equals(entity.getFile_type_id()) ||
                            "57".equals(entity.getFile_type_id()) ||
                            "58".equals(entity.getFile_type_id()) ||
                            "59".equals(entity.getFile_type_id()) ||
                            "60".equals(entity.getFile_type_id()) ||
                            "61".equals(entity.getFile_type_id()) ||
                            "62".equals(entity.getFile_type_id()) ||
                            "63".equals(entity.getFile_type_id()) ||
                            "64".equals(entity.getFile_type_id()) ||
                            "65".equals(entity.getFile_type_id()) ||
                            "66".equals(entity.getFile_type_id()) ||
                            "67".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;
                    }
                }
                mAdapter.setNewData(newList);
                break;
        }
    }

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task_borrower_accessory_f;
    }

    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        mViewBinding.recyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
        mPresenter.setId(id);
    }

    @Override
    protected void initData() {
        super.initData();
        mList = new ArrayList<>();
        mRequest = new TaskAccessoryInfoRequest();
        mAdapter = new AccessoryInfoAdapter(_mActivity, new ArrayList<>());
        mViewBinding.recyclerView.setAdapter(mAdapter);
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mViewBinding.setClickHandler(new ClickHandler());
        mAdapter.setOnAddPicClickListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    /**
     * 返回附件信息1信息
     *
     * @param list
     * @param marriage_status
     */
    @Override
    public void returnAccessoryInfoData(List<TaskAccessoryInfoEntity> list, String marriage_status) {
        mList = list;
        this.marriage_status = marriage_status;
        List<TaskAccessoryInfoEntity> newList = new ArrayList<>();
        newList.addAll(mList);
        switch (this.marriage_status) {
            case "1":
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("50".equals(entity.getFile_type_id()) ||
                            "51".equals(entity.getFile_type_id()) ||
                            "52".equals(entity.getFile_type_id()) ||
                            "53".equals(entity.getFile_type_id()) ||
                            "54".equals(entity.getFile_type_id()) ||
                            "55".equals(entity.getFile_type_id()) ||
                            "56".equals(entity.getFile_type_id()) ||
                            "57".equals(entity.getFile_type_id()) ||
                            "58".equals(entity.getFile_type_id()) ||
                            "59".equals(entity.getFile_type_id()) ||
                            "60".equals(entity.getFile_type_id()) ||
                            "61".equals(entity.getFile_type_id()) ||
                            "62".equals(entity.getFile_type_id()) ||
                            "63".equals(entity.getFile_type_id()) ||
                            "64".equals(entity.getFile_type_id()) ||
                            "65".equals(entity.getFile_type_id()) ||
                            "66".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;

                    }
                }
                mAdapter.setNewData(newList);
                break;
            case "2"://已婚
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("56".equals(entity.getFile_type_id()) ||
                            "57".equals(entity.getFile_type_id()) ||
                            "58".equals(entity.getFile_type_id()) ||
                            "59".equals(entity.getFile_type_id()) ||
                            "60".equals(entity.getFile_type_id()) ||
                            "61".equals(entity.getFile_type_id()) ||
                            "62".equals(entity.getFile_type_id()) ||
                            "63".equals(entity.getFile_type_id()) ||
                            "64".equals(entity.getFile_type_id()) ||
                            "65".equals(entity.getFile_type_id()) ||
                            "66".equals(entity.getFile_type_id()) ||
                            "67".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;
                    }
                }
                mAdapter.setNewData(newList);
                break;
            case "3"://再婚
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("50".equals(entity.getFile_type_id()) ||
                            "51".equals(entity.getFile_type_id()) ||
                            "52".equals(entity.getFile_type_id()) ||
                            "53".equals(entity.getFile_type_id()) ||
                            "54".equals(entity.getFile_type_id()) ||
                            "55".equals(entity.getFile_type_id()) ||
                            "56".equals(entity.getFile_type_id()) ||
                            "57".equals(entity.getFile_type_id()) ||
                            "58".equals(entity.getFile_type_id()) ||
                            "59".equals(entity.getFile_type_id()) ||
                            "67".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;
                    }
                }
                mAdapter.setNewData(newList);
                break;
            case "4"://离异
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("50".equals(entity.getFile_type_id()) ||
                            "51".equals(entity.getFile_type_id()) ||
                            "52".equals(entity.getFile_type_id()) ||
                            "53".equals(entity.getFile_type_id()) ||
                            "54".equals(entity.getFile_type_id()) ||
                            "55".equals(entity.getFile_type_id()) ||
                            "58".equals(entity.getFile_type_id()) ||
                            "59".equals(entity.getFile_type_id()) ||
                            "60".equals(entity.getFile_type_id()) ||
                            "61".equals(entity.getFile_type_id()) ||
                            "62".equals(entity.getFile_type_id()) ||
                            "63".equals(entity.getFile_type_id()) ||
                            "64".equals(entity.getFile_type_id()) ||
                            "65".equals(entity.getFile_type_id()) ||
                            "66".equals(entity.getFile_type_id()) ||
                            "67".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;
                    }
                }
                mAdapter.setNewData(newList);
                break;
            case "5"://丧偶
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("50".equals(entity.getFile_type_id()) ||
                            "51".equals(entity.getFile_type_id()) ||
                            "52".equals(entity.getFile_type_id()) ||
                            "53".equals(entity.getFile_type_id()) ||
                            "54".equals(entity.getFile_type_id()) ||
                            "55".equals(entity.getFile_type_id()) ||
                            "56".equals(entity.getFile_type_id()) ||
                            "57".equals(entity.getFile_type_id()) ||
                            "60".equals(entity.getFile_type_id()) ||
                            "61".equals(entity.getFile_type_id()) ||
                            "62".equals(entity.getFile_type_id()) ||
                            "63".equals(entity.getFile_type_id()) ||
                            "64".equals(entity.getFile_type_id()) ||
                            "65".equals(entity.getFile_type_id()) ||
                            "66".equals(entity.getFile_type_id()) ||
                            "67".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;
                    }
                }
                mAdapter.setNewData(newList);
                break;
            default:
                for (int i = 0; i < newList.size(); i++) {
                    TaskAccessoryInfoEntity entity = newList.get(i);
                    if ("50".equals(entity.getFile_type_id()) ||
                            "51".equals(entity.getFile_type_id()) ||
                            "52".equals(entity.getFile_type_id()) ||
                            "53".equals(entity.getFile_type_id()) ||
                            "54".equals(entity.getFile_type_id()) ||
                            "55".equals(entity.getFile_type_id()) ||
                            "56".equals(entity.getFile_type_id()) ||
                            "57".equals(entity.getFile_type_id()) ||
                            "58".equals(entity.getFile_type_id()) ||
                            "59".equals(entity.getFile_type_id()) ||
                            "60".equals(entity.getFile_type_id()) ||
                            "61".equals(entity.getFile_type_id()) ||
                            "62".equals(entity.getFile_type_id()) ||
                            "63".equals(entity.getFile_type_id()) ||
                            "64".equals(entity.getFile_type_id()) ||
                            "65".equals(entity.getFile_type_id()) ||
                            "66".equals(entity.getFile_type_id()) ||
                            "67".equals(entity.getFile_type_id())) {
                        newList.remove(entity);
                        i--;
                    }
                }
                mAdapter.setNewData(newList);
                break;
        }
    }


    @Override
    public void onAddPicClick(BaseQuickAdapter adapter, int parentPos, int position, View v) {
        String file_type_id = mAdapter.getData().get(parentPos).getFile_type_id();
        if ("48".equals(file_type_id)) {
            PickerViewHelper.showAudioList(_mActivity, which -> {
                switch (which) {
                    case 0:
                        startCamera(parentPos, 1);
                        break;
                    case 1:
                        openGallery(parentPos, 1);
                        break;
                    default:
                        break;
                }
            });
        } else {
            PickerViewHelper.showCameraList(_mActivity, which -> {
                switch (which) {
                    case 0:
                        startCamera(parentPos, 0);
                        break;
                    case 1:
                        openGallery(parentPos, 0);
                        break;
                    default:
                        break;
                }
            });
        }
    }

    private void startCamera(int parentPos, int type) {
        this.parentPos = parentPos;
        Bundle bundle = new Bundle();
        bundle.putString("watertitle", ((FTaskDetailActivity) getActivity()).getWatertitle());
        // 单独拍照
        PictureSelector.create(FTaskBorrowerAccessoryFragment.this)
                .openCamera(type == 0 ? PictureMimeType.ofImage() : PictureMimeType.ofAudio())// 单独拍照，也可录像或也可音频 看你传入的类型是图片or视频
                .theme(R.style.picture_default_style)// 主题样式设置 具体参考 values/styles
                .isSystemCamera(false)
                .maxSelectNum(20)// 最大图片选择数量
                .minSelectNum(0)// 最小选择数量
                .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
                .waterContent(((FTaskDetailActivity) getActivity()).getWatertitle())
                .previewImage(true)// 是否可预览图片
                .previewVideo(true)// 是否可预览视频
                .enablePreviewAudio(true) // 是否可播放音频
                .compressGrade(Luban.THIRD_GEAR)// luban压缩档次，默认3档 Luban.FIRST_GEAR、Luban.CUSTOM_GEAR
                .isCamera(false)// 是否显示拍照按钮
                .enableCrop(false)// 是否裁剪
                .compress(false)// 是否压缩
                .compressMode(PictureConfig.LUBAN_COMPRESS_MODE)//系统自带 or 鲁班压缩 PictureConfig.SYSTEM_COMPRESS_MODE or LUBAN_COMPRESS_MODE
                .glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                .previewEggs(false)//预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中)
                .forResult(PictureConfig.CHOOSE_REQUEST, bundle);//结果回调onActivityResult code
    }

    private void openGallery(int parentPos, int type) {
        this.parentPos = parentPos;
        Bundle bundle = new Bundle();
        bundle.putString("watertitle", ((FTaskDetailActivity) getActivity()).getWatertitle());
        // 单独拍照
        PictureSelector.create(FTaskBorrowerAccessoryFragment.this)
                .openGallery(type == 0 ? PictureMimeType.ofImage() : PictureMimeType.ofAudio())// 单独拍照，也可录像或也可音频 看你传入的类型是图片or视频
                .theme(R.style.picture_default_style)// 主题样式设置 具体参考 values/styles
                .maxSelectNum(9)// 最大图片选择数量
                .minSelectNum(1)// 最小选择数量
                .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
                .waterContent(((FTaskDetailActivity) getActivity()).getWatertitle())
                .previewImage(true)// 是否可预览图片
                .previewVideo(true)// 是否可预览视频
                .enablePreviewAudio(true) // 是否可播放音频
                .compressGrade(Luban.THIRD_GEAR)// luban压缩档次，默认3档 Luban.FIRST_GEAR、Luban.CUSTOM_GEAR
                .isCamera(false)// 是否显示拍照按钮
                .enableCrop(false)// 是否裁剪
                .compress(true)// 是否压缩
                .compressMode(PictureConfig.LUBAN_COMPRESS_MODE)//系统自带 or 鲁班压缩 PictureConfig.SYSTEM_COMPRESS_MODE or LUBAN_COMPRESS_MODE
                .glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                .previewEggs(false)//预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中)
                .forResult(PictureConfig.CHOOSE_REQUEST, bundle);//结果回调onActivityResult code
    }

    @Override
    public void onAddPicClick(int layoutPosition) {
        String file_type_id = mAdapter.getData().get(layoutPosition).getFile_type_id();
        if ("48".equals(file_type_id)) {
            PickerViewHelper.showAudioList(_mActivity, which -> {
                switch (which) {
                    case 0:
                        startCamera(parentPos, 1);
                        break;
                    case 1:
                        openGallery(parentPos, 1);
                        break;
                    default:
                        break;
                }
            });
        } else {
            PickerViewHelper.showCameraList(_mActivity, which -> {
                switch (which) {
                    case 0:
                        startCamera(layoutPosition, 0);
                        break;
                    case 1:
                        openGallery(layoutPosition, 0);
                        break;
                    default:
                        break;
                }
            });
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, int parentPos, int position, View v) {
        this.parentPos = parentPos;
        this.position = position;
        if ("48".equals(mAdapter.getData().get(parentPos).getFile_type_id())) {
            MiPicturePlayAudioActivity.externalPicturePreview(this, _mActivity, position, mAdapter.getData().get(parentPos).getFile_content().get(position),
                    StringUtils.isEmpty(mAdapter.getData().get(parentPos).getFile_name()) ? mAdapter.getData().get(parentPos).getFile_type_name() : mAdapter.getData().get(parentPos).getFile_name());
        } else {
            MiPictureExternalPreviewActivity.externalPicturePreview(this, _mActivity, position,
                    mAdapter.getData().get(parentPos).getFile_content(),
                    StringUtils.isEmpty(mAdapter.getData().get(parentPos).getFile_name()) ? mAdapter.getData().get(parentPos).getFile_type_name() : mAdapter.getData().get(parentPos).getFile_name());
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    boolean isCompressed = false;
                    boolean eqAudio = false;
                    // 图片选择结果回调
                    List<LocalMedia> localMedias = PictureSelector.obtainMultipleResult(data);
                    if (localMedias.size() == 0) {
                        return;
                    } else {
                        eqAudio = PictureMimeType.isAudio(localMedias.get(0).getPath());
                        isCompressed = localMedias.get(0).isCompressed();
                    }
                    if (!isCompressed && !eqAudio) {
                        compressImage(localMedias);
                    } else {
                        uploadPic(localMedias, false);
                    }
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，已取压缩路径为准，因为是先裁剪后压缩的
                    break;
                case MiPictureExternalPreviewActivity.DELETE_FILE:
                    List<TaskAccessoryInfoEntity.FileContentBean> images = (List<TaskAccessoryInfoEntity.FileContentBean>) data.getSerializableExtra("images");
                    mAdapter.getData().get(parentPos).getFile_content().clear();
                    mAdapter.getData().get(parentPos).getFile_content().addAll(images);
                    mAdapter.notifyItemChanged(parentPos);
                    break;
                case MiPicturePlayAudioActivity.DELETE_FILE_AUDIO:
                    mAdapter.getData().get(parentPos).getFile_content().remove(position);
                    mAdapter.notifyItemChanged(parentPos);
                    break;
            }
        }
    }

    private void uploadPic(List<LocalMedia> localMedias, boolean isShowDialog) {
        List<File> files = new ArrayList<>();
        List<TaskAccessoryInfoEntity.FileContentBean> fileContentBeanList = new ArrayList<>();
        for (int i = 0; i < localMedias.size(); i++) {
            LocalMedia localMedia = localMedias.get(i);
            TaskAccessoryInfoEntity.FileContentBean entity = new TaskAccessoryInfoEntity.FileContentBean();
            entity.setPath(localMedia.getPath());
            entity.setChecked(localMedia.isChecked());
            entity.setCompressed(localMedia.isCompressed());
            entity.setCompressPath(localMedia.getCompressPath());
            entity.setCut(localMedia.isCut());
            entity.setCutPath(localMedia.getCutPath());
            entity.setDuration(localMedia.getDuration());
            entity.setHeight(localMedia.getHeight());
            entity.setMimeType(localMedia.getMimeType());
            entity.setWidth(localMedia.getWidth());
            entity.setPictureType(localMedia.getPictureType());
            entity.setPosition(localMedia.getPosition());
            entity.setHeight(localMedia.getHeight());
            entity.setNum(localMedia.getNum());
            files.add(new File(StringUtils.isEmpty(localMedia.getCompressPath()) ? localMedia.getPath() : localMedia.getCompressPath()));
            fileContentBeanList.add(entity);
        }
        Observable.create(new ObservableOnSubscribe<Progress>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Progress> e) throws Exception {
                OkGo.<ApiResult<List<UpLoadPicEntity>>>post(ApiConstant.UTIL_UPLOAD)
                        .tag(this)
                        .params("id", id)
                        .params("userid", UserInfoSp.getInstance().getUserid())
                        .params("type", mAdapter.getData().get(parentPos).getFile_type_id())
                        .addFileParams("mcchuploadfiles[]", files)
                        .execute(new JsonCallback<ApiResult<List<UpLoadPicEntity>>>() {
                            @Override
                            public void onError(Response<ApiResult<List<UpLoadPicEntity>>> response) {
                                super.onError(response);
                                e.onError(response.getException());
                            }

                            @Override
                            public void onSuccess(Response<ApiResult<List<UpLoadPicEntity>>> response) {
                                e.onComplete();
                                ApiResult<List<UpLoadPicEntity>> body = response.body();
                                List<UpLoadPicEntity> data = body.data;
                                for (int i = 0; i < data.size(); i++) {
                                    UpLoadPicEntity entity = data.get(i);
                                    if ("1".equals(entity.getStatus())) {
                                        fileContentBeanList.get(i).setFile_path(entity.getMsg());
                                        fileContentBeanList.get(i).setFile_id(entity.getData());
                                    }
                                }
                                mAdapter.getData().get(parentPos).getFile_content().addAll(fileContentBeanList);
                                mAdapter.notifyItemChanged(parentPos);

                            }

                            @Override
                            public void uploadProgress(Progress progress) {
                                e.onNext(progress);
                            }
                        });
            }
        })
                .compose(RxSchedulers.observableIoToMain())
                .subscribeWith(new MiSubScriber<Progress>() {
                    @Override
                    protected void doOnStart() {
                        if (!isShowDialog) {
                            LoadDialogUtil.showMaterLoading(_mActivity, "");
                        }
                    }

                    @Override
                    protected void doOnNext(Progress progress) {

                    }

                    @Override
                    protected void doOnError(ApiException e) {
                        super.doOnError(e);
                        LoadDialogUtil.hideMaterLoading();
                    }

                    @Override
                    protected void doOnComplete() {
                        LoadDialogUtil.hideMaterLoading();
                    }
                });
    }

    /**
     * compressImage
     */
    protected void compressImage(final List<LocalMedia> result) {
        LoadDialogUtil.showMaterLoading(_mActivity, "");
        // luban压缩
        LubanOptions option = new LubanOptions.Builder()
                .setGrade(Luban.THIRD_GEAR)
                .create();
        CompressConfig compress_config = CompressConfig.ofLuban(option);
        CompressImageOptions.compress(_mActivity, compress_config, result,
                new CompressInterface.CompressListener() {
                    @Override
                    public void onCompressSuccess(List<LocalMedia> images) {
                        uploadPic(images, true);
                    }

                    @Override
                    public void onCompressError(List<LocalMedia> images, String msg) {
                        uploadPic(images, true);
                    }
                }).compress();
    }

    public class ClickHandler {
        public void onSave(View view) {

        }
    }
}
